package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.annotation.Log;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysRole;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.menu.BusinessType;
import com.base.vistter.iframe.service.SysRoleService;
import com.base.vistter.iframe.utils.IoUtils;
import com.base.vistter.iframe.utils.ToolUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/role")
public class SysRoleController {

    @Resource(name = "sysRoleService")
    private SysRoleService sysRoleService;

    @GetMapping
    public Result<Page<SysRole>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String name) throws IframeException {
        Page<SysRole> page = Page.of(pageNo, pageNum);
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper<SysRole>();
        if (StringUtils.isNotEmpty(name)) {
            lambdaQueryWrapper.like(SysRole::getName, name);
        }
        if(!ToolUtils.isAdmin()){
            lambdaQueryWrapper.eq(SysRole::getIsAdmin, 0);
        }
        return Result.ok(sysRoleService.page(page, lambdaQueryWrapper));
    }

    @GetMapping("/{id}")
    public Result<SysRole> get(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysRoleService.getById(id));
    }

    @Log(title = "新增角色", businessType = BusinessType.SAVE)
    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody SysRole sysRole) throws IframeException {
        sysRole.setIsAdmin(0);
        return Result.ok(sysRoleService.save(sysRole));
    }

    @Log(title = "修改角色", businessType = BusinessType.UPDATE)
    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysRole sysRole) throws IframeException {
        return Result.ok(sysRoleService.update(sysRole));
    }

    @Log(title = "删除角色", businessType = BusinessType.DELETE)
    @DeleteMapping(produces = "application/json;charset=utf-8")
    public Result<Integer> delete(@RequestBody List<String> ids) throws IframeException {
        return Result.ok(sysRoleService.delete(ids));
    }

    @Log(title = "删除角色", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(sysRoleService.delete(id));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IframeException {
        List<SysRole> list = sysRoleService.selectList();
        IoUtils.importExcel("角色列表", "角色列表", response, SysRole.class, list);
    }

    @GetMapping("/list")
    public Result<List<SysRole>> getList() throws IframeException {
        return Result.ok(sysRoleService.selectList());
    }
}
